﻿/* 
 *  <copyright file="AngleJointCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;
using SobrietyEngine.Actor;
using SobrietyEngine.Guids;
using SobrietyEngine.Logging;
using SobrietyEngine.Physics;

namespace SobrietyEngine.Components.Physics
{
    public class AngleJointCmp : BaseComponent, SobrietyEngine.Components.IComponent
    {
        protected FarseerPhysicsService physicsGameService;

        protected String physicsWorldName = "";
        protected World world;

        Boolean attachToSelf = false;
        IActor ia1, ia2;
        CGuid a1, a2;

        float targetAngle = 0.0f;

        public AngleJoint Joint { get; set; }

        public AngleJointCmp()
            : base()
        {
            //can have many joints
            this.SingleInstance = false;
        }

        //constructor should take bodies, IActors, ActorGuids, and FarseerPhysicsCmps
        public AngleJointCmp(String _physicsWorldName, IActor actor1, IActor actor2, float angle)
            : this()
        {
            targetAngle = angle;
            attachToSelf = false;
            ia1 = actor1;
            ia2 = actor2;
            physicsWorldName = _physicsWorldName;
        }

        public AngleJointCmp(String _physicsWorldName, IActor otherActor, float angle)
            : this()
        {
            attachToSelf = true;
            ia2 = otherActor;
            physicsWorldName = _physicsWorldName;
            targetAngle = angle;
        }


        public AngleJointCmp(String _physicsWorldName, CGuid actor1, CGuid actor2, float angle)
            : this()
        {
            attachToSelf = false;
            a1 = actor1;
            a2 = actor2;
            physicsWorldName = _physicsWorldName;
            targetAngle = angle;
        }

        //for attaching to the parnt component
        public AngleJointCmp(String _physicsWorldName, CGuid otherActor, float angle)
            : this()
        {
            attachToSelf = true;
            a2 = otherActor;
            physicsWorldName = _physicsWorldName;
            targetAngle = angle;
        }

        public override Boolean InitComponent()
        {
            base.InitComponent();

            //get physics world
            physicsGameService = Actor.GameServices().PhysicsGameService;
            world = physicsGameService.Worlds[physicsWorldName];
            
            //get the icmp


            IFarseerPhysicsBody ic1 = null;
            IFarseerPhysicsBody ic2 = null;

            //get based on guids
            if (a2 != null)
            {
                if (attachToSelf)
                    a1 = Actor.Guid;

                ic1 = (((IActor)a1.Resolve()).Get<IFarseerPhysicsBody>());
                ic2 = (((IActor)a2.Resolve()).Get<IFarseerPhysicsBody>());

                if (ic1 == null || ic2 == null)
                    Logger.GetSingleton().WriteError(this, "Need to create physics bodies on linke actors bfore initializing this component");
            }
            else if (ia2 != null)
            {
                if (attachToSelf)
                    ia1 = Actor;

                ic1 = ia1.Get<IFarseerPhysicsBody>();
                ic2 = ia2.Get<IFarseerPhysicsBody>();
                if (ic1 == null || ic2 == null)
                    Logger.GetSingleton().WriteError(this, "Need to create physics bodies on linke actors bfore initializing this component");
            }

            //get the actual component
            FarseerPhysicsCmp c1 = (FarseerPhysicsCmp)ic1;
            FarseerPhysicsCmp c2 = (FarseerPhysicsCmp)ic2;

            if (c1 == null || c2 == null || c1.Body == null || c2.Body == null)
                return false; //need physics bodies, cant initialize yet, delay our intialization

            //create joint
            Joint = JointFactory.CreateAngleJoint(world, c1.Body, c2.Body);
            Joint.TargetAngle = (float)Math.PI;            

            return true;
        }
    }
}